package cn.com.elite;

import java.io.File;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Date;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class Delete_tempfile implements Runnable{
	private static final Log log=LogFactory.getLog(Delete_tempfile.class);
	public static String eml_path="";  //存放临时EML文件的地�?
	private int judgeNum=0;
	public static String getEml_path() {
		return eml_path;
	}

	public static void setEml_path(String eml_path) {
		Delete_tempfile.eml_path = eml_path;
	}
	private String getERRO(Exception e){
		String str="";
		try{
            StringWriter sw = new StringWriter();  
            e.printStackTrace(new PrintWriter(sw, true));  
            str = sw.toString(); 	
		}catch(Exception e1){
			str=e.toString();
			log.error("getERRO:"+e1.toString());
		}
		return str;
	}
	private void delAllFile(String path)throws Exception{
		File file=new File(path);
		String[] tempList=file.list();
		File temp=null;
		for(int i=0;i<tempList.length;i++){
			 if(path.endsWith(File.separator)){
				 temp=new File(path+tempList[i]);
			 }else{
				 temp=new File(path+File.separator+tempList[i]);
			 }
			 if(temp.isFile()){
				 temp.delete();
			 }
			 if(temp.isDirectory()){
				 delAllFile(path+"/"+tempList[i]);//先删除文件夹里面的文件
	             delFolder(path+"/"+tempList[i]);//再删除空文件夹
			 }
		}
	}
	private void delFolder(String folderPath)throws Exception{
		delAllFile(folderPath); //删除完里面所有内容
	}
	public void run(){
		while(true){
			try{
				Date date=new Date();
				String nowtime=String.format("%tR",date); //获取现在几点
				nowtime=nowtime.substring(0,2);
				if(nowtime.equals("01")&&judgeNum==0){
					log.info("开始清扫临时文件");
					delFolder(eml_path);
					judgeNum=0;
				}
			}catch(Exception e){
				log.info("run()失败:"+getERRO(e));
			}		
			try{
				Thread.sleep(50*60*1000);
			}catch(Exception e){
				log.info("Thread.sleep失败:"+getERRO(e));
			}	
		}
		
	}

}
